package com.player.dao.util;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;
import com.player.dao.DAOLocator;
import com.player.entity.Player;
import com.player.entity.Ranking;
import com.player.entity.RankinglistType;
import com.player.rest.exception.PropertyAlreadyExistsException;
import com.player.rest.exception.ServiceException;

public class DummyDataBuilder {
	public void build(int createPlayerCount) {
		for (int i = 0; i < createPlayerCount; i++) {
			try {
				Player player = DAOLocator.getPlayerDAO().create("email" + i + "@gmail.com", "Playername" + i);
				Ranking ranking = DAOLocator.getRankingDAO().create(player.getId(), RankinglistType.MEN);
				ranking.setPoints(new Long(i * 10 + 100));
				DAOLocator.getRankingDAO().save(ranking);
			} catch (PropertyAlreadyExistsException e) {
				e.printStackTrace();
			} catch (ServiceException e) {
				e.printStackTrace();
			}
		}
	}

	public void cleanAll() {
		removeAll(Player.class, Ranking.class);
	}

	private void removeAll(Class<?>... classes) {
		for (Class<?> clazz : classes) {
			Objectify ofy = ObjectifyService.begin();
			Query<?> query = ofy.query(clazz);
			ofy.delete(query);
		}
	}
}
